package com.stefanie.kafkademo.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author: stefanie-zy
 * @time: 2023/12/13-15:40
 * @description: Kafka测试控制层
 */
@RestController
@RequestMapping(value = "/test")
@RequiredArgsConstructor
public class KafkaProducerController {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public final String TOPIC_NAME = "stefanie-zy-topic";

    /**
     * 向特定主题发送消息
     */
    @GetMapping(value = "/send")
    public String send() {
        kafkaTemplate.send(TOPIC_NAME, 0, "key-1", "this is the first kafka message and time is " + new Date());
        return "send success!";
    }
}
